package cn.sc.summer.auth.handler;

import cn.hutool.extra.spring.SpringUtil;
import cn.sc.summer.constant.model.Result;
import cn.sc.summer.constant.util.ConvertUtil;
import cn.sc.summer.token.service.EncryptService;
import cn.sc.summer.token.util.ResponseUtil;
import lombok.extern.slf4j.Slf4j;
import org.springframework.security.core.Authentication;
import org.springframework.security.web.authentication.logout.LogoutSuccessHandler;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.HashMap;

/**
 * 类名：登出成功处理类
 *
 * @author a-xin
 * @date 2023/11/28 13:56
 */
@Slf4j
public class LogoutSuccessHandlerX implements LogoutSuccessHandler {

    @Override
    public void onLogoutSuccess(HttpServletRequest request, HttpServletResponse response, Authentication authentication) throws IOException {
        EncryptService encryptService = SpringUtil.getBean(EncryptService.class);
        String encrypt = encryptService.encrypt(request, ConvertUtil.toJsonString(Result.success()));

        log.warn("==> User has successfully logged out!");
        ResponseUtil.writeSuccess(response, new HashMap<>(), encrypt);
    }

}
